package com.syf.config.exception;

import com.syf.config.result.ResultCodeEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

/**
 * @Classname BizException
 * @Description 自定义异业务异常，如果业务需要抛出异常，请使用此类
 */
@EqualsAndHashCode(callSuper = true)  //注解在类，重写hashCode和equals方法。
@Data
public class BizException extends RuntimeException {
    private Integer code;
    private String msg;
    private Object data;
    private ResultCodeEnum resultCode;  //异常码(枚举)

    /**
     * 抛出自定义异常：
     *      throw new BizException(ControllerExceptionEnum.URL_NOT_EXIST);
     *      自定义异常码(所有异常枚举Enum 都要实现 ResultCodeEnum，否则无法赋值)
     */
    public BizException(ResultCodeEnum resultCode) {
        super(resultCode.getMsg());
        this.resultCode = resultCode;
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
    }

    public BizException(Integer code, String errorMessage) {
        super(errorMessage); //RuntimeException
        this.code = code;
        this.msg = errorMessage;
    }

    public BizException(ResultCodeEnum resultCode, String additionalMsg) {
        this(resultCode);
        this.msg = this.getMsg() + ": " + additionalMsg;
    }
}
